/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtmcraft.graphics;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import jtmcraft.game.GameMap;
import jtmcraft.game.communication.TextMessage;

/**
 *
 * @author tom
 */
public class Renderer {

    BufferedImage emptybuffor;
    BufferedImage backbuffor;
    Graphics2D bbGd2;
    Graphics2D ebGd2;
    private int width;
    private int height;
    private GraphicsConfiguration graphicsConfiguration;
    private GameMap gameMap = null;
    boolean init = false;

    public Renderer(int width, int height, GraphicsConfiguration graphicsConfiguration, GameMap gameMap) {
        this.width = width;
        this.height = height;
        this.graphicsConfiguration = graphicsConfiguration;
        this.gameMap = gameMap;
    }

    public void Init() {
        backbuffor = graphicsConfiguration.createCompatibleImage(width, height);
        emptybuffor = graphicsConfiguration.createCompatibleImage(width, height);
        // backbuffor = panel.getGraphicsConfiguration().createCompatibleImage( 500, 500 );
        // emptybuffor = panel.getGraphicsConfiguration().createCompatibleImage( 500, 500 );
        bbGd2 = backbuffor.createGraphics();
        ebGd2 = emptybuffor.createGraphics();

        ebGd2.setColor(Color.red);
        ebGd2.fillRect(0, 0, emptybuffor.getWidth(), emptybuffor.getHeight());

        init = true;
        clearBackBuffer();


    }

    public void clearBackBuffer() {
        if (!init) {
            return;
        }

        if (gameMap != null) {
            gameMap.drawMap(bbGd2);
        } else {
            bbGd2.drawImage(emptybuffor, 0, 0, null);
        }
    }

    /*
    public void drawSprite(List<Sprite> sprites, int x, int y, int width, int height, boolean focused) {
        if (!init) {
            return;
        }
        for (Sprite sprite : sprites) {
            drawSprite(sprite, x, y, width, height, focused);
        }
    }
     *
     */

    public void drawSprite(Sprite sprite, int x, int y, int width, int height,
                           Color playerColor, int durability, int maxDurability ) {
        if (!init) {
            return;
        }
        bbGd2.drawImage(sprite.getImage(), x - gameMap.getxOffset(), y - gameMap.getyOffset(), null);
        
        
        if( playerColor != null )
        {
            int durWidth = (int) ((float) width * ( (float) durability / (float) maxDurability )) + 1;
            bbGd2.setColor(playerColor);
            bbGd2.drawRect(x - gameMap.getxOffset(), y - gameMap.getyOffset() - 5 , width + 1, 3);
            bbGd2.fillRect(x - gameMap.getxOffset(), y - gameMap.getyOffset() - 5 , durWidth, 3);
        }

    }

    public void drawSelectedBox( Sprite sprite, int x, int y, int width, int height )
    {
        bbGd2.setColor(Color.WHITE );
        bbGd2.drawRect( x - gameMap.getxOffset(), y - gameMap.getyOffset(), width, height);
    }


    public void update(Graphics2D gd2) {
        if (!init) {
            Init();
        }

        gd2.drawImage(backbuffor, 0, 0, null);
    }

    public void drawMessage( ArrayList< TextMessage > messages )
 {
        if (!init) {
            return;
        }
        bbGd2.setColor(Color.WHITE);

        int x = 20;
        int y = 560;

        int yoffset = 20;
        
        for( int i = messages.size() - 1, j = 0; i >= 0 && j < 6 ; ++j, i--)
        {
            bbGd2.drawString(messages.get(i).getMessage(), x, y - j * yoffset);
        }

    }

}
